package com.mydiceplayer.utils;
import java.text.DecimalFormat;
import java.text.NumberFormat;
import java.util.Formatter;
import java.util.Locale;

public class UtilFile {

	public static String calculateSize(long size){
		
        double mSize = ((double) size / 1024.0);
        String flag;
        if (mSize > 1024.0) {
            mSize = mSize / 1024.0;
            flag = "M";
        } else {
            flag = "K";
        }

        NumberFormat formater = DecimalFormat.getInstance();
        formater.setMaximumFractionDigits(1);
        return formater.format(mSize) + flag;
	}

	public static String calculateDuration(long duration) {
		int totalSeconds = (int) (duration / 1000);
		int seconds = totalSeconds % 60;
		int minutes = (totalSeconds / 60);
		
		StringBuilder formatBuilder = new StringBuilder();
		Formatter formatter = new Formatter(formatBuilder, Locale.getDefault());		
		formatBuilder.setLength(0);
		if (minutes > 0) {
			return formatter.format("%d:%02d", minutes, seconds).toString();
		} else {
			if (seconds == 0)
				seconds = 1;

			return formatter.format("00:%02d",seconds).toString();
		}
	}
}
